/**
 * 
 */
package ro.dta.idbi.patterns.primitives;

import java.util.EnumSet;

import ro.dta.idbi.interfaces.IntegrationOperation;
import ro.dta.idbi.model.AbstractPrimitivePattern;
import uk.ac.ic.doc.automed.IntegrityException;
import uk.ac.ic.doc.automed.NotFoundException;
import uk.ac.ic.doc.automed.reps.Schema;
import uk.ac.ic.doc.automed.reps.SchemaObject;
import uk.ac.ic.doc.automed.reps.TypeMismatchException;

/**
 * <b>Primitive transformation to delete a SQL construct</b><br />
 * 
 * Example of use:<br />
 * 
 * <code>delete (table:&lt;&lt;tableName&gt;&gt;, iql)</code><br />
 * <code>delete (column:&lt;&lt;tableName,columnName,optionality,type&gt;&gt;, iql)</code>
 * <br />
 * <code>delete (primarykey:&lt;&lt;pk_name,table,&lt;&lt;table,col1&gt;&gt;, ..., &lt;&lt;table,coln&gt;&gt;&gt;&gt;)</code>
 * <br />
 * <code>delete (foreignkey:&lt;&lt;fk_name,sourceTbl,&lt;&lt;sourceTbl,col1&gt;&gt;, ..., &lt;&lt;sourceTbl,coln&gt;&gt;,targetTbl,&lt;&lt;targetTbl,col1&gt;&gt;, ..., &lt;&lt;targetTbl,coln&gt;&gt;&gt;&gt;)</code>
 * 
 * @author Tudor Dobrila
 * 
 */
public class DeletePrimitive extends AbstractPrimitivePattern {

	/**
	 * Name of the pattern
	 */
	public static final String NAME = "Delete construct";

	/**
	 * Description of the pattern
	 */
	public static final String DESCRIPTION = "Delete an existing construct";

	/**
	 * Name of the command associated with the pattern
	 */
	public static final String COMMAND = "delete";

	/**
	 * Integration phase during which the command is applied
	 */
	public static final EnumSet<IntegrationOperation> OPERATION = EnumSet.of(
			IntegrationOperation.CONFORMING, IntegrationOperation.MERGING,
			IntegrationOperation.IMPROVEMENT);

	/**
	 * Add a new construct class constructor
	 * 
	 * @param sourceSchema
	 *            Source schema over which the transformation is applied
	 */
	public DeletePrimitive(final Schema sourceSchema) throws NotFoundException, IntegrityException {
		super(sourceSchema, COMMAND);
	}

	/**
	 * Add a new construct class constructor
	 * 
	 * @param sourceSchema
	 *            Name of source schema over which the transformation is applied
	 */
	public DeletePrimitive(final String sourceSchema) throws NotFoundException, IntegrityException {
		this(Schema.getSchema(sourceSchema));
	}

	@Override
	public Schema execute(String trans) throws NotFoundException, TypeMismatchException,
			IntegrityException {
		super.execute(trans);
		
		Schema crtSchema = sourceSchema;

		String type = (String) construct.get("type");
		SchemaObject remObj = null;
		String extent = null;
		if ("table".equals(type)) {
			String name = (String) construct.get("name");
			extent = (String) construct.get("iql");
			remObj = crtSchema.getSchemaObject("<<" + name + ">>");
		} else if ("column".equals(type)) {
			String table = (String) construct.get("table");
			String name = (String) construct.get("name");
			extent = (String) construct.get("iql");
			remObj = crtSchema.getSchemaObject("<<" + table + "," + name + ">>");
		} else if ("primarykey".equals(type) || "foreignkey".equals(type)) {
			String name = (String) construct.get("fulldef");
			remObj = crtSchema.getSchemaObject(name);
		}

		if (remObj != null) {
			crtSchema = crtSchema.applyDeleteTransformation(remObj, extent, null);
		}

		return crtSchema;
	}

}
